#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    d_samplingThread = nullptr;
    d_plot = new Plot(this);
    d_plot->setIntervalLength();
    ui->verticalLayout->addWidget(d_plot);
    //    test = nullptr;

    ui->comboBox->addItem(tr("Светлая тема"));
    ui->comboBox->addItem(tr("Тёмная тема"));
    ui->comboBox->addItem(tr("Серая тема"));
    ui->comboBox->setCurrentIndex(0);
    d_plot->setTheme(ui->comboBox->currentIndex());
    connect( ui->comboBox, SIGNAL( currentIndexChanged( int ) ),
             d_plot, SLOT(setTheme(int)));

    d_plot->replot();
}

MainWindow::~MainWindow()
{
    stopSamplinThread();
    delete ui;

}

void MainWindow::start()
{
//    d_samplingThread.init();
    d_samplingThread->start();
    d_plot->start();
}

void MainWindow::on_pushButton_clicked()
{
//    d_plot->decrementInterval();
    stopSamplinThread();
    int numConnection = 1;
    Isi *test = new Isi(numConnection,this);

    if(!test->open())
    {
        delete test;
        return;
    }
    test->setPath();
    qDebug()<<"1) тут";
    qDebug()<<"open? " << test->open();
    d_params = test->getParams();
//    ui->statusbar->showMessage(test->path());
    ui->lineEdit->setText(test->path());
    qDebug() << "_______________________________________";
    qDebug()<< "d_samplingthread" << d_samplingThread;
    d_samplingThread = new SamplingThread();

    d_samplingThread->setPath(test->path());
    delete test;
    d_samplingThread->setCountParam(d_params);
    d_samplingThread->setNumConnection(numConnection);
    d_samplingThread->init();
    d_plot->addCurves(d_params);
//    delete test;
    start();
}

void MainWindow::stopSamplinThread()
{
    if(d_samplingThread!=nullptr)
    {
        d_plot->killTimer();
        d_plot->decrementInterval();
        d_samplingThread->stop();
        d_samplingThread->wait(1000);
        delete d_samplingThread;
        d_samplingThread = nullptr;
        d_plot->clearCurves();
    }
}
